package jogo.sprite;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;

/**
 * A sprite to be displayed on the screen. Note that a sprite contains no state
 * information, i.e. its just the image and not the location. This allows us to
 * use a single sprite in lots of different places without having to store
 * multiple copies of the image.
 *
 * @author Kevin Glass
 */
public class Sprite {

    /**
     * The image to be drawn for this sprite
     */
    private BufferedImage image;
    private int posInix = 0, posIniy = 0;
    private int increMentox = 0;
    private int increMentoy = 0;
    private int posFimx = 1024, posFimy = 1024;
    private int largura = 64;
    private int altura = 64;
    private int frames = 16;
    private int frameAtual = 0;
    private long previewTime = 0;
    private long speed = 40;
    private int animacao;
    private boolean linha2 = false;//Quantidade de linahs que a animação possui
    public boolean vivo = false;

    /**
     * Create a new sprite based on an image
     *
     * @param image The image that is this sprite
     */
    public Sprite(BufferedImage image) {
        this.image = image;
    }

    public BufferedImage getImage() {
        return image;
    }

    public int getAnimacao() {
        return animacao;
    }

    public void setAnimacao(int animacao) {
        this.animacao = animacao;
    }

    public boolean isVivo() {
        return vivo;
    }

    public void setVivo(boolean vivo) {
        this.vivo = vivo;
    }

    public void setImage(BufferedImage image) {
        this.image = image;
    }

    public int getLargura() {
        return largura;
    }

    public void setLargura(int largura) {
        this.largura = largura;
    }

    public int getAltura() {
        return altura;
    }

    public void setAltura(int altura) {
        this.altura = altura;
    }

    public int getFrames() {
        return frames;
    }

    public void setFrames(int frames) {
        this.frames = frames;
    }

    public int getFrameAtual() {
        return frameAtual;
    }

    public void setFrameAtual(int frameAtual) {
        this.frameAtual = frameAtual;
    }

    public long getPreviewTime() {
        return previewTime;
    }

    public void setPreviewTime(long previewTime) {
        this.previewTime = previewTime;
    }

    public long getSpeed() {
        return speed;
    }

    public void setSpeed(long speed) {
        this.speed = speed;
    }

    /**
     * Get the width of the drawn sprite
     *
     * @return The width in pixels of this sprite
     */
    public int getWidth() {
        return image.getWidth(null);
    }

    /**
     * Get the height of the drawn sprite
     *
     * @return The height in pixels of this sprite
     */
    public int getHeight() {
        return image.getHeight(null);
    }

    public void upDate(Graphics2D g, int x, int y, long time) {

        if (previewTime + speed < time) {

            if (frameAtual == (frames -1) ) {                
                frameAtual = 0;

            } else {
                if(frames > 1)
                    frameAtual++;
            }



            previewTime = time;

            if (((posInix + (frameAtual * (largura + (frameAtual == 0 ? 0 : increMentox)))) >= getPosFimx())) {
                //Se possuir 2 linhas de frame
                if (posFimy > posIniy) {
                    linha2 = !linha2;
                    frameAtual = 0;
                }                
            }
            
        }

        //Dobrei a proporção do tamanho na image
        g.drawImage(image.getSubimage(posInix + (frameAtual * (largura + (frameAtual == 0 ? 0 : increMentox))), ((linha2) ? posFimy : posIniy), largura, altura), x, y, (largura * 2), (altura * 2), null);

    }

    /**
     * @return the posInix
     */
    public int getPosInix() {
        return posInix;
    }

    /**
     * @param posInix the posInix to set
     */
    public void setPosInix(int posInix) {
        this.posInix = posInix;
    }

    /**
     * @return the posIniy
     */
    public int getPosIniy() {
        return posIniy;
    }

    /**
     * @param posIniy the posIniy to set
     */
    public void setPosIniy(int posIniy) {
        this.posIniy = posIniy;
    }

    /**
     * @return the posFimx
     */
    public int getPosFimx() {
        return posFimx;
    }

    /**
     * @param posFimx the posFimx to set
     */
    public void setPosFimx(int posFimx) {
        this.posFimx = posFimx;
    }

    /**
     * @return the posFimy
     */
    public int getPosFimy() {
        return posFimy;
    }

    /**
     * @param posFimy the posFimy to set
     */
    public void setPosFimy(int posFimy) {
        this.posFimy = posFimy;
    }

    /**
     * @return the linha2
     */
    public boolean isLinha2() {
        return linha2;
    }

    /**
     * @param linha2 the linha2 to set
     */
    public void setLinha2(boolean linha2) {
        this.linha2 = linha2;
    }

    /**
     * @return the increMentox
     */
    public int getIncreMentox() {
        return increMentox;
    }

    /**
     * @param increMentox the increMentox to set
     */
    public void setIncreMentox(int increMentox) {
        this.increMentox = increMentox;
    }

    /**
     * @return the increMentoy
     */
    public int getIncreMentoy() {
        return increMentoy;
    }

    /**
     * @param increMentoy the increMentoy to set
     */
    public void setIncreMentoy(int increMentoy) {
        this.increMentoy = increMentoy;
    }
}
